﻿using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace XiaoSe.Web.Net
{
    public class Mail
    {
        private readonly SmtpClient _smtp;
        public Mail(string host, string userName, string userPsw)
        {
            _smtp = new SmtpClient
                {
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    EnableSsl = false,
                    Host = host,
                    Port = 25,
                    Credentials = new NetworkCredential(userName, userPsw)
                }; //实例化一个SmtpClient
            //如果需要认证，则用下面的方式
        }
        public void SendMail(string fromAddress, string toAddress, string subject, string body)
        {
            var mm = new MailMessage
                {
                    Priority = MailPriority.High,
                    Subject = subject,
                    From = new MailAddress(fromAddress)
                }; //实例化一个邮件类
            mm.To.Add(new MailAddress(toAddress));
            mm.IsBodyHtml = true; //邮件正文是否是HTML格式
            mm.BodyEncoding = Encoding.UTF8;
            //邮件正文的编码， 设置不正确， 接收者会收到乱码
            mm.Body = body;
            _smtp.Send(mm);
        }
        public void SendMail(string fromAddress, IEnumerable<string> toAddress, string subject, string body)
        {
            var mm = new MailMessage
                {
                    Priority = MailPriority.High,
                    Subject = subject,
                    From = new MailAddress(fromAddress)
                }; //实例化一个邮件类
            foreach (var to in toAddress)
            {
                mm.To.Add(new MailAddress(to));
            }
            mm.IsBodyHtml = true; //邮件正文是否是HTML格式
            mm.BodyEncoding = Encoding.UTF8;
            //邮件正文的编码， 设置不正确， 接收者会收到乱码
            mm.Body = body;
            _smtp.Send(mm);
        }
    }
}
